<template>
  <div id="check">
    <div class="top">
      <el-button size="mini" type="primary" plain @click="Button_on_add()">新建盘点单</el-button>
      <el-button
        size="mini"
        type="primary"
        :disabled="dis.edit"
        plain
        @click="Button_on_edit()"
      >编辑盘点单</el-button>
      <el-button
        size="mini"
        type="primary"
        :disabled="dis.delete"
        plain
        @click="Button_on_delete()"
      >删除盘点单</el-button>
      <el-button
        size="mini"
        type="primary"
        :disabled="dis.submit"
        plain
        @click="Button_on_submit()"
      >提交盘点单</el-button>

      <div class="top-check">
        <el-form :inline="true" :model="checkForm" size="mini">
          <el-form-item label="盘点单编码">
            <el-input v-model="checkForm.checkRef"></el-input>
          </el-form-item>
          <el-form-item label="类型">
            <el-select v-model="checkForm.typename  " placeholder="请选择类型">
              <el-option label="验货盘点" value="验货盘点"></el-option>
              <el-option label="采购盘点" value="采购盘点"></el-option>
              <el-option label="归还盘点" value="归还盘点"></el-option>
            </el-select>
          </el-form-item>

          <el-form-item label="状态">
            <el-select v-model="checkForm.statu" placeholder="请选择状态">
              <el-option label="已盘点" value="已盘点"></el-option>
              <el-option label="待盘点" value="待盘点"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="业务日期">
            <el-date-picker
              type="date"
              placeholder="选择日期"
              v-model="checkForm.date"
              value-format="yyyy-MM-dd"
              style="width: 100%;"
            ></el-date-picker>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="CheckSubmit">查询</el-button>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="CheckClear">清空查询条件</el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
    <div class="body">
      <el-table
        border
        :data="check_list"
        :stripe="true"
        @row-click="selectRow"
        :header-cell-style="{background:'#e3e5e9',color:'#606266'}"
        :row-style="{height:'5px'}"
        :cell-style="{padding:'5px 0'}"
        tooltip-effect="light"
        highlight-current-row
        height="400"
      >
        <el-table-column label="序号" width="50">
          <template slot-scope="scope">
            <span>{{scope.$index + 1}}</span>
          </template>
        </el-table-column>
        <el-table-column prop="checkRef" label="编号" width="200"></el-table-column>
        <el-table-column prop="statu" label="状态" width="95"></el-table-column>
        <el-table-column prop="typename" label="类型" width="155"></el-table-column>
        <el-table-column prop="date" label="业务日期" width="205"></el-table-column>
        <el-table-column prop="storename" label="盘点仓库" width="205"></el-table-column>
        <el-table-column prop="bizname" label="业务员" width="150"></el-table-column>
        <el-table-column prop="returnRef" label="归还单号" width="200"></el-table-column>
        <el-table-column prop="calRef" label="采购入库单号" width="200"></el-table-column>
      </el-table>
    </div>
    <div class="detail">
      <el-tabs type="border-card">
        <el-tab-pane>
          <span slot="label">
            <i class="el-icon-s-order"></i>
            单号:{{ref}} 盘点仓库:{{storename}}
          </span>
          <div>
            <el-table
              border
              :data="detaillist"
              size="mini"
              :row-style="{height:'5px'}"
              :cell-style="{padding:'5px 0'}"
            >
              <el-table-column label="序号" width="50">
                <template slot-scope="scope">
                  <span>{{scope.$index + 1}}</span>
                </template>
              </el-table-column>
              <el-table-column label="商品编码" width="250" prop="code"></el-table-column>
              <el-table-column label="商品名称" prop="name" width="200"></el-table-column>
              <el-table-column show-overflow-tooltip prop="spec" label="规格型号" width="250"></el-table-column>
              <el-table-column show-overflow-tooltip prop="count" label="盘点后数量" width="95"></el-table-column>
              <el-table-column show-overflow-tooltip label="单位" prop="unitname" width="125"></el-table-column>
              <el-table-column prop="memo" show-overflow-tooltip label="备注" width="410"></el-table-column>
            </el-table>
          </div>
        </el-tab-pane>
      </el-tabs>
    </div>
    <CheckAdd
      v-if="visible_add"
      @dialog-close="visible_add = false"
      @refresh="refreshCheckList"
      :form="selectedRow"
    ></CheckAdd>
    <CheckEdit
      v-if="visible_edit"
      @dialog-close="visible_edit = false"
      @refresh="refreshCheckList"
      :form="selectedRow"
    ></CheckEdit>
    <div class="submit">
      <el-dialog
        title="确定归还"
        :visible.sync="visible_submit"
        :modal-append-to-body="false"
        :close-on-press-escape="false"
        :close-on-click-modal="false"
        width="500px"
      >
        <p>
          你确定要提交盘点单
          <strong>{{selectedRow.checkRef}}</strong>
          吗？
        </p>

        <div slot="footer" class="dialog-footer">
          <el-button @click="visible_submit = false">取 消</el-button>
          <el-button type="primary" @click="Submit_Submit()">确 定</el-button>
        </div>
      </el-dialog>
    </div>
    <div class="delete">
      <el-dialog
        title="确定删除"
        :visible.sync="visible_delete"
        :modal-append-to-body="false"
        :close-on-press-escape="false"
        :close-on-click-modal="false"
        width="500px"
      >
        <div class="notify">
          <p>
            <strong>注意：</strong>
            <br />确定归还后，不能删除该盘点单
          </p>
        </div>
        <p>
          你确定要删除
          <strong>{{selectedRow.checkRef}}</strong>
          吗？
        </p>

        <div slot="footer" class="dialog-footer">
          <el-button @click="visible_delete = false">取 消</el-button>
          <el-button type="primary" @click="Submit_Delete()">确 定</el-button>
        </div>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import CheckAdd from "./check_dialog/add";
import CheckEdit from "./check_dialog/edit";
export default {
  components: { CheckAdd, CheckEdit },
  data() {
    return {
      ref: "IT151516",
      storename: "IT151516",
      visible_add: false,
      visible_edit: false,
      visible_delete: false,
      visible_submit: false,
      dis: {
        edit: true,
        delete: true,
        submit: true
      },
      checkForm: {},
      check_list: [],
      detaillist: [],
      selectedRow: {}
    };
  },
  created() {
    this.refreshCheckList();
  },
  methods: {
    CheckClear() {
      this.checkForm = {};
    },
    CheckSubmit() {
      this.$http.post("assetCheck/search", this.checkForm).then(response => {
       this.check_list = response.data;
      });
    },
    selectRow(Row) {
      this.$http
        .post("assetCheckDetail/list", { checkId: Row.id })
        .then(response => {
          this.detaillist = response.data;
          window.console.log(Row);
          window.console.log("detail", this.detaillist);
        });
      this.selectedRow = JSON.parse(JSON.stringify(Row));

      //根据状态和类型阻止一些功能
      if (Row.statu == "已盘点") {
        this.dis.delete = true;
        this.dis.edit = true;
        this.dis.submit = true;
      } else if (Row.statu == "待盘点") {
        if (Row.typename == "验货盘点") {
          this.dis.delete = false;
          this.dis.edit = false;
        } else {
          this.dis.delete = true;
          this.dis.edit = false;
        }
        this.dis.submit = false;
      }
    },
    refreshCheckList() {
      this.$http.get("assetCheck/list").then(response => {
        this.check_list = response.data;
      });
    },
    check() {
      if (this.selectedRow.id == null || this.selectedRow == "") {
        this.$message.error("请选择一个盘点单");
        return false;
      } else {
        return true;
      }
    },
    Button_on_add() {
      this.visible_add = true;
    },
    Button_on_edit() {
      if (this.check()) {
        this.visible_edit = true;
      }
    },
    Button_on_delete() {
      if (this.check()) {
        this.visible_delete = true;
      }
    },
    Button_on_submit() {
      if (this.check()) {
        this.visible_submit = true;
      }
    },

    Submit_Delete() {
      this.$http
        .post("assetCheck/delete", { id: this.selectedRow.id })
        .then(response => {
          if (response.data == "success") {
            this.$message({
              type: "success",
              message: "删除完成"
            });
            this.visible_delete = false;
            this.refreshCheckList();
          }
        });
    },
    Submit_Submit() {
      this.selectedRow.statu = "已盘点";
      this.detaillist.forEach(Row => {
        this.$http
          .post("good/someGoodList", { code: Row.code })
          .then(response => {
            Row.zijin = response.data[0].saleprice * Row.count;
            Row.statu = "在库";
            Row.goodname = Row.name;
            Row.storeid = this.selectedRow.storeid;
            Row.goodcode = Row.code;
            window.console.log("zijin", Row.zijin);
            if (Row.zijin != null) {
              let path = "assetDetail/insert";
              if (this.selectedRow.typename == "验货盘点") {
                path = "assetDetail/yanhuo";
              }
              this.$http.post(path, Row);
            }
          });
      });
      this.$http.post("assetCheck/update", this.selectedRow).then(response => {
        if (response.data == this.selectedRow.id) {
          this.$message({
            type: "success",
            message: "提交完成"
          });
          this.visible_submit = false;
          this.refreshCheckList();
        }
      });
    }
  }
};
</script>

<style>
</style>